package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.mapper.MemberMapper;
import com.itheima.health.mapper.OrderMapper;
import com.itheima.health.mapper.OrderSettingMapper;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import com.itheima.health.utils.resources.MessageConstant;
import com.itheima.health.utils.resources.RedisMessageConstant;
import com.itheima.health.utils.sms.ValidateCodeUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private OrderSettingService orderSettingService;

    @Autowired
    private MemberService memberService;

    @Override
    public void sendVerificationCode(String telephone) {
        Random random = new Random();
        int j = random.nextInt(2);
        int i;
        if(j==1){
            i=4;
        }else {
            i=6;
        }
        String s = ValidateCodeUtils.generateValidateCode(i);
        System.out.println(s);
        redisTemplate.opsForValue().set(RedisMessageConstant.SENDTYPE_ORDER+telephone,s,300, TimeUnit.SECONDS);
//        SmsUtils.sendVerificationCode(s,telephone);
    }

    @Override
    public Order add(Map map) {
        try {
            String orderDate = (String) map.get("orderDate");
            String orderType = (String) map.get("orderType");
            String setmealId = (String) map.get("setmealId");
            int sId = Integer.parseInt(setmealId);
            int count = orderSettingService.checkOrderSetting(orderDate);
            //count大于0可以预约
            if(count>0){
                //可以预约不管是不是会员都要进行订单的创建所以提到外边
                Order order = new Order();
                order.setOrderDate(DateUtils.parseString2Date(orderDate));
                order.setOrderStatus("未到诊");
                order.setOrderType(orderType);
                order.setSetmealId(sId);
                //查询是否是会员
                String telephone = (String) map.get("telephone");
                Member member =  memberService.selectIsExistByPhoneNumber(telephone);
                if(member!=null){
                    //如果是会员就要查询是否已经有相同的订单
                    Integer mid = member.getId();
                    Order orderExist = baseMapper.selectByMemberAndDate(mid,sId,orderDate);
                    if(orderExist==null) {
                        order.setMemberId(mid);
                        baseMapper.insert(order);
                        orderSettingService.updateReservations(orderDate);
                        return order;
                    }else {
                        throw new RuntimeException(MessageConstant.HAS_ORDERED);
                    }
                }else {
                    //如果不是会员就可以直接进行会员的注册，订单的添加等操作
                    String name = (String)map.get("name");
                    String sex = (String)map.get("sex");
                    String idCard = (String)map.get("idCard");
                    Member memb = new Member();
                    memb.setName(name);
                    memb.setSex(sex);
                    memb.setIdCard(idCard);
                    memb.setPhoneNumber(telephone);
                    memb.setRegTime(new Date());
                    memberService.save(memb);
                    order.setMemberId(memb.getId());
                    baseMapper.insert(order);
                    orderSettingService.updateReservations(orderDate);
                    return order;
                }
            }else {
                //否则当日预约已满不能预约
                throw  new RuntimeException(MessageConstant.ORDER_FULL);
            }
        } catch (RuntimeException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public Map successOrder(int id) {
        return baseMapper.successOrder(id);
    }
}
